import { Component, Inject } from '@angular/core';
import { IonicPage, NavController, ModalController } from 'ionic-angular';

// 枚举
import { FamilyTiesEnum, SocietyTiesEnum } from '../../../../config/enum';
// picker
import { convertEnumToColumn } from 'ion-multi-picker';
import { Subscription } from 'rxjs/Subscription';
import { Contacts } from '../../../../providers/entity/user.model';
// 常量
import * as PageConstant from '../../../../constant/pageConstant';
/**
 * 紧急联系人
 */
@IonicPage()
@Component({
  selector: 'page-mine-contacts',
  templateUrl: 'mine-contacts.html'
})
export class MineContactsPage{

  // 家庭关系
  private initFamily: any;
  private FamilyTies: Array<any>;
  private family: Contacts;

  // 社会关系
  private initSociety: any;
  private SocietyTies: Array<any>;
  private friend: Contacts;

  // 监听
  private userSubscription: Subscription;

  constructor(
    @Inject('user') private userService,
    private navCtrl: NavController,
    public modalCtrl: ModalController,
  )
  {
    this.initData();
  }

  /*** 初始化数据
  * */
  initData()
  {
    this.userSubscription= this.userService.getUser()
      // .filter(user=> JSON.stringify(user.contacts)!=='{}')
      // .filter(user=> Object.keys(user.contacts).length !==0)
      .map(user=> user.contacts)
      .subscribe(contacts=>
      {
        let familyRelation;
        let friendRelation;
        try{
          this.family= contacts.emergency;
          this.friend= contacts.other;

          let r1 = contacts.emergency.relation, r2 = contacts.other.relation;

          familyRelation= r1? r1: '父亲';
          friendRelation= r2? r2: '朋友';
        }catch(err) {
          familyRelation= '父亲';
          friendRelation= '朋友';
        }finally {
          this.initPicker(familyRelation,friendRelation);
        }

      })
  }

  ionViewWillEnter()
  {
  }
  ionViewDidLoad(){
    // this.contcatsService.getLocalTestList();
    // this.contcatsService.getContcatsList();
  }

  /*** 页面离开
   * */
  ionViewWillLeave()
  {
    if(typeof this.userSubscription !== 'undefined')
      this.userSubscription.unsubscribe();
  }

  /*** 初始化picker
   * @param familyRelation 紧急关系
   * @param friendRelation 其他联系
   * */
  private initPicker(familyRelation: string,friendRelation: string): void
  {
    this.initFamily = FamilyTiesEnum[familyRelation];
    this.FamilyTies = convertEnumToColumn(FamilyTiesEnum);

    this.initSociety = SocietyTiesEnum[friendRelation];
    this.SocietyTies = convertEnumToColumn(SocietyTiesEnum);
  }

  /*** 选择直属亲属
  * */
  protected choiceFamily(): void
  {
    let modal = this.modalCtrl.create(PageConstant.ContcatsPage, {},{
      showBackdrop: false,
      enableBackdropDismiss: false,
    });
    modal.onDidDismiss(family => {
      if(!family) return;
      this.family= {...family};
    });
    modal.present();
  }

  /*** 选择其他联系人
  * */
  protected choiceFriend(): void
  {
    let modal = this.modalCtrl.create(PageConstant.ContcatsPage, {},{
      showBackdrop: false,
      enableBackdropDismiss: false,
    });
    modal.onDidDismiss(friend => {
      if(!friend) return;
      this.friend= {...friend};
    });
    modal.present();
  }

  /*** 去保存信息
  * */
  doSaveContactsInfo()
  {
    const familyRelation= this.FamilyTies[0].options[this.initFamily].text;

    const family = this.family;
    const emergency= this.assembleList(familyRelation,family);

    const friendRelation= this.SocietyTies[0].options[this.initSociety].text;
    const friend = this.friend;

    const other= this.assembleList(friendRelation,friend);
    const contact= { emergency, other };
    const contactState= 1;

    const options= { contact, contactState };
    // 增加保存参数
    this.userService.updateConacts(options, this.navCtrl);
  }

  /*** 组件信息
   * @param relation  关系
   * @param info      电话簿关系
  * */
  private assembleList(relation: string, info: any): any
  {
    let name= info.name;
    let phone= info.phone;
    let data= { name, phone, relation};
    return data;
  }

}

